Micron Document
Livres et Wikis | Archives | Info


JavaScript syntax
part 4/43 Β· 161.3 KB total
layout: Wide Β· Narrow Β· Centered
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
// Treated as:
// a = b + c(d + e).foo();

with the suggestion that the preceding statement be terminated with a
semicolon.

Some suggest instead the use of leading semicolons on lines starting
with '(' or '[', so the line is not accidentally joined with the
previous one. This is known as a defensive semicolon, and is
particularly recommended, because code may otherwise become ambiguous
when it is rearranged. For example:

a = b + c
;(d + e).foo()
// Treated as:
// a = b + c;
// (d + e).foo();

Initial semicolons are also sometimes used at the start of JavaScript
libraries, in case they are appended to another library that omits a
trailing semicolon, as this can result in ambiguity of the initial
statement.

The five restricted productions are return, throw, break, continue, and
post-increment/decrement. In all cases, inserting semicolons does not
fix the problem, but makes the parsed syntax clear, making the error
easier to detect. return and throw take an optional value, while break
and continue take an optional label. In all cases, the advice is to keep
the value or label on the same line as the statement. This most often
shows up in the return statement, where one might return a large object
literal, which might be accidentally placed starting on a new line. For
post-increment/decrement, there is potential ambiguity with
pre-increment/decrement, and again it is recommended to simply keep
these on the same line.

return
a + b;
// Returns undefined. Treated as:
// return;
// a + b;
// Should be written as:
// return a + b;

Comments

Comment syntax is the same as in C++, Swift and other programming
languages.

Single-line comments begin with // and continue until the end of the
line. A second type of comments can also be made; these start with /*
and end with */ and can be used for multi-line comments.

A third type of comment, the hashbang comment, starts with #! and
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────